package com.yunshipei.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Window;
import android.view.WindowManager;

import com.yunshipei.common.Globals;
import com.yunshipei.common.UserLoginDataParser;
import com.yunshipei.core.utils.SystemUtils;
import com.yunshipei.enterplorer.BuildConfig;
import com.yunshipei.enterplorer.R;
import com.yunshipei.manager.YspPreferences;
import com.yunshipei.model.MainExtraBean;
import com.yunshipei.utils.BaseUtil;
import com.yunshipei.utils.EncUtils;
import com.yunshipei.utils.FileUtils;

import java.util.TimerTask;

/**
 * 启动页面，这个里面做了一些相关的兼容工作，客户端兼容旧版和新版服务器接口的处理逻辑
 */
public class SplashActivity extends Activity {

    private SharedPreferences mPreferences;
    private SharedPreferences mNoClearSP;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
        setContentView(R.layout.activity_splash);
        this.mPreferences = YspPreferences.getInstance().getSharedPreferences();
        this.mNoClearSP = getSharedPreferences(Globals.NO_CLEAR_SHP, MODE_PRIVATE);
        mNoClearSP.edit().putString(Globals.MANUAL_BASE_URL, BuildConfig.MANUAL_BASE_URL).commit();
        mNoClearSP.edit().putString(Globals.SP_COMPANY_ID,BuildConfig.SP_COMPANY_ID).commit();

        new Handler().postDelayed(new Runnable(){

            public void run() {

                //多设备登录时，下次启动app自动跳转到登录页面
               // if (mPreferences.getBoolean(Globals.SP_MULTI_DEVICES, false)) {
                    reLogin();
//                } else {
//                    MainExtraBean mainExtraBean = loadCache();
//                    if (mainExtraBean != null) {
//                        startActivity(new MainActivity.MainIntentBuilder(SplashActivity.this).setData(mainExtraBean).isAutoLogin(true).getIntent());
//                    } else {
//                        reLogin();
//                    }
//                }
            }

        }, 2000);

       // finish();
    }

    private MainExtraBean loadCache() {
        String managerServer = mNoClearSP.getString(Globals.MANUAL_BASE_URL, "");
        if (TextUtils.isEmpty(managerServer)) {
            managerServer = mNoClearSP.getString(Globals.SP_HUB_GET_SERVER, "");
        }
        String companyId = mNoClearSP.getString(Globals.SP_COMPANY_ID, "");
        String uuid = mPreferences.getString(Globals.UUID, "");
        String userName = mPreferences.getString(Globals.USER_NAME, "");
        String password = "";
        String cacheLoginData = "";
        if (!TextUtils.isEmpty(uuid)) {
            String tempPassword = mPreferences.getString(Globals.PASSWORD, "");
            if (!TextUtils.isEmpty(tempPassword)) {
                password = EncUtils.decrypt(uuid, tempPassword);
            }
            String tempLoginCacheData = mPreferences.getString(Globals.SP_LOGIN_DATA, "");
            if (!TextUtils.isEmpty(tempLoginCacheData)) {
                cacheLoginData = EncUtils.decrypt(uuid, tempLoginCacheData);
            }
        }
        MainExtraBean mainExtraBean = null;
        if (!TextUtils.isEmpty(managerServer) && !TextUtils.isEmpty(companyId) && !TextUtils.isEmpty(userName) && !TextUtils.isEmpty(password) || !TextUtils.isEmpty(cacheLoginData)) {
            mainExtraBean = new UserLoginDataParser(this).parse(cacheLoginData);
        }
        return mainExtraBean;
    }

    //失败之后跳转到登录
    private void reLogin() {
        BaseUtil.deletePathReal(this.getFilesDir().getPath().replace("files", "app_xwalkcore"));
        startActivity(new Intent(this,  LoginActivity.class ));

        finish();
    }
}
